#include "paramui.h"
#include "ui_paramui.h"
#include "constants.h"
#include "infoui.h"
#include <QTimer>
#include <QDateTime>
#include <QMessageBox>

//init global value
QString ParamUI::qstrDianya = "";
QString ParamUI::qstrMaikuan = "";
QString ParamUI::qstrCishu = "";
QString ParamUI::qstrJiange = "";
QString ParamUI::qstrLiusu = "";
bool ParamUI::blYaodong = false;
bool ParamUI::blValidate = false;
float ParamUI::fTimeSpan = 0;
int ParamUI::iVolume = 220;

ParamUI::ParamUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ParamUI),
    iDxEdit(0)
{
    ui->setupUi(this);
    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint );
    //load logo and background
    QPixmap lg(logoStr);
    ui->logo->setPixmap(lg);
    QPixmap bg(backgroundStr);
    QPalette background;
    background.setBrush(QPalette::Background, QBrush(bg));
    setPalette(background);
    QPalette palette = ui->data->palette();
    palette.setColor(backgroundRole(), Qt::white);
    ui->data->setPalette(palette);
    ui->data->setAutoFillBackground(true);
    //set font color
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);
    ui->cTime->setPalette(pe);
    ui->lbltitle->setPalette(pe);
    //button style
    QPalette btnPe;
    btnPe.setColor(QPalette::ButtonText, Qt::white);
    ui->btnRet->setStyleSheet(blueBtn);
    ui->btn0->setStyleSheet(blueBtn);
    ui->btn1->setStyleSheet(blueBtn);
    ui->btn2->setStyleSheet(blueBtn);
    ui->btn3->setStyleSheet(blueBtn);
    ui->btn4->setStyleSheet(blueBtn);
    ui->btn5->setStyleSheet(blueBtn);
    ui->btn6->setStyleSheet(blueBtn);
    ui->btn7->setStyleSheet(blueBtn);
    ui->btn8->setStyleSheet(blueBtn);
    ui->btn9->setStyleSheet(blueBtn);
    ui->btnDot->setStyleSheet(blueBtn);
    ui->btnC->setStyleSheet(blueBtn);
    ui->btnD->setStyleSheet(blueBtn);
    ui->btnOk->setStyleSheet(greenBtn);
    ui->btnRet->setPalette(btnPe);
    ui->btnOk->setPalette(btnPe);
    ui->btn0->setPalette(btnPe);
    ui->btn1->setPalette(btnPe);
    ui->btn2->setPalette(btnPe);
    ui->btn3->setPalette(btnPe);
    ui->btn4->setPalette(btnPe);
    ui->btn5->setPalette(btnPe);
    ui->btn6->setPalette(btnPe);
    ui->btn7->setPalette(btnPe);
    ui->btn8->setPalette(btnPe);
    ui->btn9->setPalette(btnPe);
    ui->btnDot->setPalette(btnPe);
    ui->btnC->setPalette(btnPe);
    ui->btnD->setPalette(btnPe);
    ui->btnD->setIcon(QIcon(delStr));
    ui->btnD->setIconSize(QSize(48, 20));
    //line edit
    ui->tDianya->setStyleSheet("QLineEdit {border:1px solid "
                               ";background-color:rgb(211,211,211);border-radius:5px;selection-color:pink}");
    ui->tMaikuan->setStyleSheet("QLineEdit {border:1px solid "
                               ";background-color:rgb(211,211,211);border-radius:5px;selection-color:pink}");
    ui->tCishu->setStyleSheet("QLineEdit {border:1px solid "
                               ";background-color:rgb(211,211,211);border-radius:5px;selection-color:pink}");
    ui->tJiange->setStyleSheet("QLineEdit {border:1px solid "
                               ";background-color:rgb(211,211,211);border-radius:5px;selection-color:pink}");
    ui->tLiusu->setStyleSheet("QLineEdit {border:1px solid "
                               ";background-color:rgb(211,211,211);border-radius:5px;selection-color:pink}");
    ui->cYaodong->setStyleSheet("QCheckBox::indicator {width: 48px; height: 48px;}"
                                "QCheckBox::indicator:unchecked {image: url(C://Users/Evan/Documents/EttaBiotech/EttaF1/img/uncheck.png);}"
                                "QCheckBox::indicator:checked {image: url(C://Users/Evan/Documents/EttaBiotech/EttaF1/img/select.png);}");

    //install filter
    ui->tDianya->installEventFilter(this);
    ui->tMaikuan->installEventFilter(this);
    ui->tCishu->installEventFilter(this);
    ui->tJiange->installEventFilter(this);
    ui->tLiusu->installEventFilter(this);
    ui->tVolume->installEventFilter(this);

    QValidator *myvalidator=new QDoubleValidator(0.00,84.00,2,this);
    ui->tLiusu->setValidator(myvalidator);

    QTimer *upTime = new QTimer(this);
    connect(upTime,SIGNAL(timeout()),this,SLOT(updateTime()));
    updateTime();
    upTime->start(1000);

    ui->tDianya->setFocus();

    ui->label_12->setVisible(false);
    ui->tVolume->setVisible(false);
}

ParamUI::~ParamUI()
{
    delete ui;
}

void ParamUI::updateTime()
{
    QDateTime curTime = QDateTime::currentDateTime();
    QString lblTm = curTime.date().toString("yyyy/MM/dd") + "\n " + curTime.time().toString("hh:mm:ss");
    this->ui->cTime->setText(lblTm);
}

void ParamUI::on_btnRet_clicked()
{
    this->hide();
    emit retMain();
}

void ParamUI::getShow()
{
    this->show();
    this->activateWindow();
    this->raise();
    this->setFocus();
    ui->tDianya->setFocus();
    //set value
    ui->tDianya->setText(qstrDianya);
    ui->tMaikuan->setText(qstrMaikuan);
    ui->tCishu->setText(qstrCishu);
    ui->tJiange->setText(qstrJiange);
    ui->tLiusu->setText(qstrLiusu);
    ui->cYaodong->setChecked(blYaodong);
}

void ParamUI::on_btnOk_clicked()
{
    QString qTmp = ui->tDianya->text();
    if (qTmp.toInt() < 1 || qTmp.toInt() > 400)
    {
        InfoUI ifu;
        ifu.InfoDialog();
        ifu.SetInfo(tr("Voltage exceed limit"), tr("Voltage exceed limit（1-400 V）\n  Please re-enter!"));
        ifu.exec();
        return;
    }
    qTmp = ui->tMaikuan->text();
    if (qTmp.toInt() < 10 || qTmp.toInt() > 2000)
    {
        InfoUI ifu;
        ifu.InfoDialog();
        ifu.SetInfo(tr("Pulse width exceed limit"), tr("Pulse width exceed limit（10-2000 us）\n  Please re-enter!"));
        ifu.exec();
        return;
    }
    qTmp = ui->tCishu->text();
    if (qTmp.toInt() < 1 || qTmp.toInt() > 99)
    {
        InfoUI ifu;
        ifu.InfoDialog();
        ifu.SetInfo(tr("Repetition exceed limit"), tr("Repetition exceed limit（1-99）\n  Please re-enter!"));
        ifu.exec();
        return;
    }
    qTmp = ui->tJiange->text();
    if (qTmp.toInt() < 10 || qTmp.toInt() > 9999)
    {
        InfoUI ifu;
        ifu.InfoDialog();
        ifu.SetInfo(tr("Interval exceed limit"), tr("Interval exceed limit（10-9999 ms）\n  Please re-enter!"));
        ifu.exec();
        return;
    }
    //check liusu
    QString qLiu = ui->tLiusu->text();
    if ((qLiu.length() >= 1 && qLiu.at(0) == '.') || (qLiu.length() > 1 && qLiu.at(qLiu.length() - 1) == '.'))
    {
        InfoUI ifu;
        ifu.InfoDialog();
        ifu.SetInfo(tr("Velocity exceed limit"), tr("Velocity exceed limit（0.03-84 ml/min）\n  Please re-enter Repetition and Interval!"));
        ifu.exec();
        return;
    }
    float fRuDongSpd = ui->tLiusu->text().toFloat()*1000/28;
    if (fRuDongSpd > 2999 || fRuDongSpd < 1)
    {
        InfoUI ifu;
        ifu.InfoDialog();
        ifu.SetInfo(tr("Velocity exceed limit"), tr("Velocity exceed limit（0.03-84 ml/min）\n  Please re-enter Repetition and Interval!"));
        ifu.exec();
        return;
    }
    //update value
    qstrDianya = ui->tDianya->text();
    qstrMaikuan = ui->tMaikuan->text();
    qstrCishu = ui->tCishu->text();
    qstrJiange = ui->tJiange->text();
    qstrLiusu = ui->tLiusu->text();
    blYaodong = ui->cYaodong->isChecked();

    this->hide();
    emit paramSet();
}

void ParamUI::on_btn1_clicked()
{
    appendNum("1");
}

void ParamUI::on_btn2_clicked()
{
    appendNum("2");
}

void ParamUI::on_btn3_clicked()
{
    appendNum("3");
}

void ParamUI::on_btn4_clicked()
{
    appendNum("4");
}

void ParamUI::on_btn5_clicked()
{
    appendNum("5");
}

void ParamUI::on_btn6_clicked()
{
    appendNum("6");
}

void ParamUI::on_btn7_clicked()
{
    appendNum("7");
}

void ParamUI::on_btn8_clicked()
{
    appendNum("8");
}

void ParamUI::on_btn9_clicked()
{
    appendNum("9");
}

void ParamUI::on_btnC_clicked()
{
    this->setFocus();
    ui->tDianya->setFocus();
    ui->tDianya->clear();
    ui->tCishu->clear();
    ui->tMaikuan->clear();
    ui->tJiange->clear();
    ui->tLiusu->clear();
    ui->tVolume->clear();
}

void ParamUI::on_btn0_clicked()
{
    appendNum("0");
}

void ParamUI::on_btnDot_clicked()
{
    if (iDxEdit == 5) {
        appendNum(".");
    }
}

void ParamUI::on_btnD_clicked()
{
    switch (iDxEdit) {
    case 1:
        ui->tDianya->backspace();
        break;
    case 2:
        ui->tMaikuan->backspace();
        break;
    case 3:
        ui->tCishu->backspace();
        break;
    case 4:
        ui->tJiange->backspace();
        break;
    case 5:
        ui->tLiusu->backspace();
        break;
    case 6:
        ui->tVolume->backspace();
        break;
    default:
        break;
    }
}


bool ParamUI::eventFilter(QObject *target, QEvent *event)
{
    if (!event)
        return true;


    //get last
    if (event->type() == QEvent::FocusIn)
    {
        if (target == ui->tDianya)
        {
            iDxEdit = 1;
        }
        else if (target == ui->tMaikuan)
        {
            iDxEdit = 2;
        }
        else if (target == ui->tCishu)
        {
            iDxEdit = 3;
        }
        else if (target == ui->tJiange)
        {
            iDxEdit = 4;
        }
        else if (target == ui->tLiusu)
        {
            iDxEdit = 5;
        }
        else if (target == ui->tVolume)
        {
            iDxEdit = 6;
        }
    }

    return false;
}

void ParamUI::appendNum(QString qNum)
{
    switch (iDxEdit) {
    case 1:
        ui->tDianya->insert(qNum);
        break;
    case 2:
        ui->tMaikuan->insert(qNum);
        break;
    case 3:
        ui->tCishu->insert(qNum);
        break;
    case 4:
        ui->tJiange->insert(qNum);
        break;
    case 5:
        ui->tLiusu->insert(qNum);
        break;
    case 6:
        ui->tVolume->insert(qNum);
        break;
    default:
        break;
    }
}

void ParamUI::on_tMaikuan_textEdited(const QString &arg1)
{
    //Empty
    QString qJiange = ui->tJiange->text();
    QString qCishu = ui->tCishu->text();
    if (!qJiange.isEmpty()
        && !qCishu.isEmpty())
    {
        //set Liusu
        float fLiusu = (iVolume*60/1000)/((arg1.toFloat()/1000000) + (qCishu.toInt()*qJiange.toFloat()/1000));
        ui->tLiusu->setText(QString::number(fLiusu, 'f', 2));
    }
}

void ParamUI::on_tCishu_textEdited(const QString &arg1)
{
    //Empty
    QString qMaikuan = ui->tMaikuan->text();
    QString qJiange = ui->tJiange->text();
    if (!qMaikuan.isEmpty()
        && !qJiange.isEmpty())
    {
        //set Liusu
        float fLiusu = (iVolume*60/1000)/((qMaikuan.toFloat()/1000000) + (arg1.toInt()*qJiange.toFloat()/1000));
        ui->tLiusu->setText(QString::number(fLiusu, 'f', 2));
    }
}

void ParamUI::on_tJiange_textEdited(const QString &arg1)
{
    //Empty
    QString qMaikuan = ui->tMaikuan->text();
    QString qCishu = ui->tCishu->text();
    if (!qMaikuan.isEmpty()
        && !qCishu.isEmpty())
    {
        //set Liusu
        float fLiusu = (iVolume*60/1000)/((qMaikuan.toFloat()/1000000) + (qCishu.toInt()*arg1.toFloat()/1000));
        ui->tLiusu->setText(QString::number(fLiusu, 'f', 2));
    }
}

void ParamUI::on_tLiusu_textEdited(const QString &arg1)
{
    //Empty
    QString qMaikuan = ui->tMaikuan->text();
    QString qCishu = ui->tCishu->text();
    if (!qMaikuan.isEmpty()
        && !qCishu.isEmpty()
        && !arg1.isEmpty())
    {
        //set Liusu
        //float fLiusu = 33/((qMaikuan.toFloat()/1000000) + (qCishu.toInt()*arg1.toFloat()/1000));
        float fJiange = ((iVolume*60/1000)/arg1.toFloat() - (qMaikuan.toFloat()/1000000))*1000/qCishu.toInt();
        ui->tJiange->setText(QString::number((int)fJiange));
    }
}

void ParamUI::on_tVolume_textEdited(const QString &arg1)
{
    iVolume = arg1.toInt();
}
